package com.dlb.andorid.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.presenter.RegPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import static com.dlb.andorid.presenter.LoginPresenter.CODE_TYPE_FIND;
import static com.dlb.andorid.presenter.LoginPresenter.CODE_TYPE_REG;

public class RegOrForgetActivity extends BaseCommonActivity implements XContract.IRegView {

	public static final byte MODE_REG = 1;
	public static final byte MODE_FORGET = 2;
	public byte mode = MODE_REG;
	RegPresenter mPresenter;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		mode = (byte) getIntentData("mode");
	}

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new RegPresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {

	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_reg_or_forget;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		switch (mode) {
			case MODE_FORGET:
				return "忘记密码";
			case MODE_REG:
				return "注册";
		}
		return null;
	}

	@Override
	public String getPageName() {
		return null;
	}

	private View ll_agree;
	private TextView tv_submit;
	private TextView tv_get_code;
	private EditText et_phone, et_code, et_pwd;

	@Override
	protected void initViews() {
		super.initViews();

		tv_get_code = findViewById(R.id.tv_get_code);
		ll_agree = findViewById(R.id.ll_agree);
		tv_submit = findViewById(R.id.tv_submit);
		et_pwd = findViewById(R.id.et_pwd);
		et_phone = findViewById(R.id.et_phone);
		et_code = findViewById(R.id.et_code);

		switch (mode) {
			case MODE_FORGET:
				ll_agree.setVisibility(View.INVISIBLE);
				tv_submit.setText("确认");
				break;
			case MODE_REG:
				ll_agree.setVisibility(View.VISIBLE);
				tv_submit.setText("注册");
				break;
		}
		tv_submit.setOnClickListener(this);
		tv_get_code.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == tv_get_code) {
			if (checkPhone()) {
				showProgress("");
				switch (mode) {
					case MODE_FORGET:
						mPresenter.getCode(mStrPhone, CODE_TYPE_FIND);
						break;
					case MODE_REG:
						mPresenter.getCode(mStrPhone, CODE_TYPE_REG);
						break;
				}
			}
		} else if (v == tv_submit) {
			if (checkCode() && checkPhone() && checkPwd()) {
				showProgress("");
				switch (mode) {
					case MODE_FORGET:
						mPresenter.findPwd(mStrCode, mStrPhone, mStrPwd);
						break;
					case MODE_REG:
						mPresenter.regByPhone(mStrPhone, mStrPwd, mStrCode);
						break;
				}

			}
		}
	}

	String mStrPhone, mStrCode, mStrPwd;

	boolean checkPwd() {
		mStrPwd = et_pwd.getText().toString().trim();
		if (mStrPwd.isEmpty()) {
			toastMsg("请填写密码");
			return false;
		} else if (mStrPwd.length() < 6) {
			toastMsg("密码不能小于6位数");
			return false;
		}
		return true;
	}

	boolean checkCode() {
		mStrCode = et_code.getText().toString().trim();
		if (mStrCode.isEmpty()) {
			toastMsg("请填验证码号");
			return false;
		}
		return true;
	}

	boolean checkPhone() {

		mStrPhone = et_phone.getText().toString().trim();
		if (mStrPhone.isEmpty()) {
			toastMsg("请填写手机号");
			return false;
		}
		return true;
	}

	@Override
	public void onRegSuccess(String msg) {
		dismissProgress();
		toastMsg(msg);
		onBackPressed();
	}

	@Override
	public void getCodeSuccess(String msg) {
		dismissProgress();
		toastMsg(msg);
	}

	@Override
	public void getPwdSuccess(String msg) {
		dismissProgress();
		toastMsg(msg);
	}
}
